/*
 * Copyright 2009-2010 Nanjing RedOrange ltd (http://www.red-orange.cn)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package redora.client.mvp;

import com.google.gwt.place.shared.PlaceTokenizer;
import com.google.gwt.place.shared.Prefix;

/**
 * Table view part to display records in table layout. This place is tokened like:
 * #browse:pojoName:defaultFinderOrdinal
 * The defaultFinderOrdinal is found at the pojo.DefaultFinder enum.
 */
public class BrowsePlace extends Place {

    public final Integer defaultFinderOrdinal;
    public static final String prefix = "browse";

    public BrowsePlace(String name, Integer defaultFinderOrdinal) { //TODO params
        super(name);
        this.defaultFinderOrdinal = defaultFinderOrdinal;
    }

    @Prefix(prefix)
    public static class Tokenizer implements PlaceTokenizer<BrowsePlace> {
        @Override
        public String getToken(BrowsePlace place) {
            return place.name == null ? "" : place.name + ":" + place.defaultFinderOrdinal;
        }

        @Override
        public BrowsePlace getPlace(String token) {
            return token == null ? new BrowsePlace(null, null) : new BrowsePlace(token.split(":")[0], Integer.valueOf(token.split(":")[1]));
        }
    }

    /**
     * @return The url usable in the href attribute of an anchor.
     */
    @Override
    public String toString() {
        return "#" + prefix + (name == null ? "" : (":" + name + ":" + defaultFinderOrdinal));
    }
}